#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

/////////////////////// Measured inputs //////////////////////
int MSpeed;  //pwm steps per rotation so it's actually inverse
int S1Speed;
int S2Speed;

//varied outputs//////////////////////////////////////////////
char S1PWM;
char S2PWM;

///////////////////// PID loop variables /////////////////////
int S1Error;
int S1Diff;
int S1Int;

int S2Error;
int S2Diff;
int S2Int;

////////////////////////// ZNGains ////////////////////////////
int S1P;
int S1I;
int S1D;

int S2P;
int S2I;
int S2D;

// The maximum observed speed for each fan //////////////////
int S1Max;
int S2Max;
int MMax;

void main (void) {
  startup();
  while(true) {
    loop();
  }
}

void configurePWM() {
  //setup timer
  //setup pwm
  //setup interrups
}

bool checkAndClearRest() {
  por =//test reset register for POR
  //overwrite reset register
  return por;
}

void loadEEPROM() {
  MSpeed=//EEPROMValues here
  S1Speed=
  S2Speed=
  S1PWM=
  S2PWM=

///////////////////// PID loop variables /////////////////////
int S1Error;
int S1Diff;
int S1Int;

int S2Error;
int S2Diff;
int S2Int;

////////////////////////// ZNGains ////////////////////////////
int S1P;
int S1I;
int S1D;

int S2P;
int S2I;
int S2D;

// The maximum observed speed for each fan //////////////////
int S1Max;
int S2Max;
int MMax;
}

startup (void) {
  bool poweronreset = checkAndClearReset();
  configurePWM();
  if(poweronreset)
    init();
  loadEEPROM();
  //perform initialization of volatile memory
  
}

void ini (void) {
  //examine the system and record essential variables to eeprom
}

void loop (void) {
  findSpeed(){}
}

void ISR (SIGINTERRUPT0) {
  //the interrupt service routine maintains pwm settings
  //records speed if a fan has sent a new tach signal
  
}

void findMax (void) {
 
 }
 
void findSpeed () {
  
}

bool measureRates() {}

void wait (int n) {
  //wait n pwm steps
}
